package com.springbootblog.service.serviceImpl.fontend;

import com.springbootblog.dao.ArticleDao;
import com.springbootblog.pojo.Article;
import com.springbootblog.service.fontend.ElasticsearchService;
import com.springbootblog.utils.ElasticsearchUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ElasticsearchServiceImpl implements ElasticsearchService
{
    @Resource
    public RestHighLevelClient restHighLevelClient;

    @Resource
    private ArticleDao articleDao;

//    @Qualifier
//    @Resource
//    private ElasticsearchTemplate elasticsearchTemplate;

    /**
     * 创建索引
     * @param indexName
     * @return
     * @throws IOException
     */
    public Map<String, Object> createIndex(String indexName) throws IOException
    {
        if(indexName == "" || indexName.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Map<String, Object> result = ElasticsearchUtils.createIndex(restHighLevelClient, indexName);
        return result;
    }

    /**
     * 获取索引是否存在
     * @param indexName
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> indexIsExists(String indexName) throws IOException {
        if(indexName == "" || indexName.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Map<String, Object> result = ElasticsearchUtils.indexIsExists(restHighLevelClient, indexName);
        return result;
    }

    /**
     * 索引删除
     * @param indexName
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> deleteIndex(String indexName) throws IOException
    {
        if(indexName == "" || indexName.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Map<String, Object> result = ElasticsearchUtils.deleteIndex(restHighLevelClient, indexName);
        return result;
    }

    /**
     * 添加文档（单独添加）
     * @param indexName
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> addDocument(String id, String indexName, String time) throws IOException
    {
        if(indexName == "" || indexName.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Article detail;
        if(id == null || id.isEmpty())
        {
            detail = articleDao.getArticleDetailById("45");
            id = detail.getId().toString();
        }
        else
        {
            detail = articleDao.getArticleDetailById(id);
        }
        Map<String, Object> result = ElasticsearchUtils.addDocument(detail, indexName, restHighLevelClient, id, time);
        result.put("code", 1);
        result.put("msg", "操作成功！");
        return result;

    }

    /**
     * 获取文档
     * @param indexName
     * @param id
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> getDocument(String indexName, String id) throws Exception
    {
        if(indexName == "" || indexName.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Map<String, Object> result = ElasticsearchUtils.getDocument(restHighLevelClient, indexName,id);
        return result;
    }

    /**
     * 获取文档，判断是否存在
     * @param indexName
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> documentIsExists(String indexName, String id) throws Exception
    {
        if(indexName == "" || indexName.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Map<String, Object> result = ElasticsearchUtils.documentIsExists(restHighLevelClient, indexName,id);
        return result;
    }

    /**
     * 更新文档内容
     * @param indexName
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> updateDocument(String indexName, String id) throws Exception
    {
        if(indexName == "" || indexName.isEmpty() || id == null || id.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Article detail = articleDao.getArticleDetailById(id);
        Map<String, Object> result = ElasticsearchUtils.updateDocument(restHighLevelClient, indexName, id, detail);
        return result;
    }

    /**
     * 删除文档
     * @param indexName
     * @param id
     * @param time
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> deleteDocument(String indexName, String id, String time) throws Exception
    {
        if(indexName == "" || indexName.isEmpty() || id == null || id.isEmpty())
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        Map<String, Object> result = ElasticsearchUtils.deleteDocument(restHighLevelClient, indexName, id, time);
        return result;//*/
        /*List<Article> articleList = articleDao.getArticleList();
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (int i = 0; i < articleList.size(); i++)
        {
            Map<String, Object> result = ElasticsearchUtils.deleteDocument(restHighLevelClient, indexName, articleList.get(i).getId().toString(), time);
        }
        Map<String,Object> result = new HashMap<>(2);
        result.put("code", 1);
        result.put("msg", "操作成功！");
        return result;//*/
    }

    /**
     * 条件查询
     * @param indexName
     * @param time
     * @param searchString
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> mapSearch(String indexName, String time, String searchString,Integer page ,String[] field) throws Exception
    {
        if(indexName == "" || indexName.isEmpty() || searchString == null || searchString.isEmpty() )
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }
        //Map<String, Object> result = ElasticsearchUtils.mapSearch(restHighLevelClient, indexName, Long.parseLong(time), searchString,page ,elasticsearchTemplate);
        Map<String, Object> result = ElasticsearchUtils.mapSearch(restHighLevelClient, indexName, Long.parseLong(time), searchString,page,field);
        return result;
    }

    /**
     * 批量写入
     * @param time          超时时间
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> batchAddData(String time, String indexName) throws Exception
    {
        List<Article> articleList = articleDao.getArticleList();

        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (int i = 0; i < articleList.size(); i++)
        {
            Map<String, Object> item = new HashMap<>(2);
            item.put("id", articleList.get(i).getId());
            item.put("info", articleList.get(i));
            arrayList.add(item);
        }
         Map<String, Object> result = ElasticsearchUtils.batchAddData(restHighLevelClient, arrayList, time , indexName);
         return result;
    }


}
